package fire.and.near.challenger.database;

import java.util.List;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class DatabaseAsyncTask extends
		AsyncTask<Object, Void, DatabaseResponse> {

	private DatabaseQueryOptions option = null;
	private Context context = null;

	public DatabaseAsyncTask(Context context, DatabaseQueryOptions option) {
		this.option = option;
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		if (context != null) {
			((DatabaseActivity) context).setDatabaseWorking(true);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected DatabaseResponse doInBackground(Object... params) {
		if (this.context == null) {
			Log.i("in DatabaseAsyncTask", "context is null");
		} else {
			Log.i("in DatabaseAsyncTask", "context is NOT null");
		}

		DatabaseResponse response = new DatabaseResponse();
		ChallengeDatabaseHelper helper = new ChallengeDatabaseHelper(
				this.context);
		response.setOption(this.option);
		switch (this.option) {
		case COMPLETE:
			response.setSuccessful(helper
					.completeChallenge((Integer) params[0]));
			break;
		case DECLINE:
			response.setSuccessful(helper.declineChallenge((Integer) params[0]));
			break;
		case DEFAULT_CHALLENGE:
			response.setData(helper.getDefaultChallenge());
			if (response.getData() != null) {
				response.setSuccessful(true);
			}
			break;
		case DELETE_CHALLENGE:
			response.setSuccessful(helper.deleteChallenge((Integer) params[0]));
			break;
		case DELETE_LAST_PENDING:
			response.setSuccessful(helper.deleteLastPending((Long) params[0]));
			break;
		case DELETE_PENDING:
			response.setSuccessful(helper
					.deletePendingChallenges((Long) params[0]));
			break;
		case FRIENDS_LIST:
			response.setData(helper.getFriendsList());
			if (response.getData() != null) {
				response.setSuccessful(true);
			}
			break;
		case HISTORY_CHALLENGES:
			response.setData(helper.getHistoryChallenges((Long) params[0]));
			if (response.getData() != null) {
				response.setSuccessful(true);
			}
			break;
		case INSERT_CHALLENGES:
			long rowID = helper
					.insertChallenges((List<ChallengeModel>) params[0]);
			response.setData(rowID);
			response.setSuccessful(rowID != -1);
			break;
		case MY_CHALLENGES:
			response.setData(helper.getMyChallenges((Long) params[0]));
			if (response.getData() != null
					&& ((List<ChallengeModel>) response.getData()).size() > 0) {
				response.setSuccessful(true);
			}
			break;
		case PENDING_CHALLENGES:
			response.setData(helper.getPendingChallenges((Long) params[0]));
			if (response.getData() != null) {
				response.setSuccessful(true);
			}
			break;
		case SET_PENDING:
			response.setSuccessful(helper.updatePending((Long) params[0],
					(Boolean) params[1]));
			break;
		case SET_PHOTO:
			response.setSuccessful(helper.updatePhotoPath((Integer) params[0],
					(String) params[1]));
			break;
		case PENDING_FOR_FB:
			response.setData(helper.getPendingForFacebook((Long) params[0]));
			if (response.getData() != null) {
				response.setSuccessful(true);
			}
			break;
		case CHANGE_STATUS:
			response.setSuccessful(helper.setChallengeStatus(
					(Integer) params[0], (ChallengeStatus) params[1]));
			break;
		case SAVE_ANSWER:
			Log.i("ZMIQTA WE TASK", "" + params[0] + " : " + (String) params[1]);
			response.setSuccessful(helper.updateAnswer((Integer) params[0],
					(String) params[1]));
			break;
		default:
			break;
		}
		helper.close();
		return response;
	}

	@Override
	protected void onPostExecute(DatabaseResponse result) {
		if (context != null) {
			((DatabaseActivity) context).setDatabaseWorking(false);
			((DatabaseActivity) context).processDatabaseResponse(result);
		}
	}

	public enum DatabaseQueryOptions {
		INSERT_CHALLENGES, FRIENDS_LIST, DEFAULT_CHALLENGE, DECLINE, COMPLETE, SET_PENDING, MY_CHALLENGES, HISTORY_CHALLENGES, PENDING_CHALLENGES, DELETE_PENDING, DELETE_LAST_PENDING, DELETE_CHALLENGE, SET_PHOTO, PENDING_FOR_FB, CHANGE_STATUS, SAVE_ANSWER
	}
}
